<?php
namespace Admin\Controller;
use Common\Controller\AuthController;
use Think\Controller;
class AboutController extends AuthController
{

    /**
     * 关于我们
     **/
    public  function about(){
        if(IS_POST){
            $m = M('about');
            if (!$m->autoCheckToken($_POST)) {
                $this->error('表单令牌错误', 0, 0);
            }else{
                $data=I();
                if(!empty($data['images'])){
                    $data['images'] = base64ToImage($data['images']);
                }else
                    $data['images'] = $data['old_image'];

                if($result=M('about')->where(array('id'=>1))->find()){
                    if($result=$m->where(array('id'=>1))->save($data)){
                        $this->error('修改成功');
                    }
                    else{
                        $this->error('修改失败');
                    }
                }else{
                    $data['add_time']=time();
                    $data['id']=1;
                    if($result=$m->add($data)){
                        $this->error('添加成功');
                    }
                    else{
                        $this->error('添加失败');
                    }
                }
            }
        }else{
          if($result=M('about')->where(array('id'=>1))->find()){
              $this->assign('images',$result['images']);
             $this->assign('data',$result);
              $this->display();
          }else{
              $this->display();
          }
        }

    }

    /**
     * 联系我们
     **/
    public function contact(){
        if(IS_POST){
            $data=I();
            $data['updata_time']=time();
            $data['images'] =  $data['images']?base64ToImage($data['images']):$data['old_image'];
            unset($data['old_image']);
            M('contact')->where(array('id'=>1))->save($data)?$this->success('修改成功'): $this->error('修改失败');
        }else{
            $result=M('contact')->where(array('id'=>1))->find();
            $this->assign('result',$result);
            $this->display();
        }
    }

    /**
     * 商务合作
     **/
    public  function business_cooperation(){
        if(IS_POST){
            $data=I();
            $m=M('cooperation');
            if($m->where('id=1')->find()){
                $data['icon'][]=$data['images1']?base64ToImage($data['images1']):$data['old_image'];
                $data['icon'][]=$data['images2']?base64ToImage($data['images2']):$data['old_image2'];
                $data['icon'][]=$data['images3']?base64ToImage($data['images3']):$data['old_image3'];
                $data['icon'][]=$data['images4']?base64ToImage($data['images4']):$data['old_image4'];
                $datas['images']=$data['images5']?base64ToImage($data['images5']):$data['old_image5'];
                $datas['icon']=implode(',',$data['icon']);
                $datas['name']=implode(',',$data['name']);
                $datas['content']=implode(',',$data['content']);
                $datas['url']=implode(',',$data['url']);
                $m->where('id=1')->save($datas)?$this->success('修改成功'):$this->error('修改失败');
            }else{
                $data['icon'][]=$data['images1']?base64ToImage($data['images1']):$data['old_image'];
                $data['icon'][]=$data['images2']?base64ToImage($data['images2']):$data['old_image2'];
                $data['icon'][]=$data['images3']?base64ToImage($data['images3']):$data['old_image3'];
                $data['icon'][]=$data['images4']?base64ToImage($data['images4']):$data['old_image4'];
                $datas['images']=$data['images5']?base64ToImage($data['images5']):$data['old_image5'];
                $datas['icon']=implode(',',$data['icon']);
                $datas['id']=1;
                $m->add($datas)?$this->success('添加成功'):$this->error('添加失败');
            }
        }else{
            $data= M('cooperation')->where('id=1')->find();
            $data_icon=explode(',',$data['icon']);
            $data_name=explode(',',$data['name']);
            $data_content=explode(',',$data['content']);
            $data_url=explode(',',$data['url']);
            $this->assign('images',$data['images']);
            $this->assign('dataicon',$data_icon);
            $this->assign('url',$data_url);
            $this->assign('dataname',$data_name);
            $this->assign('datacontent',$data_content);
            $this->display();
        }

    }

    /**
     * 留言板
     **/
    public  function message_board(){
            header("Content-type:text/html;charset=utf-8");
            $val=I('val');
            $val? $where['tel']=array('like',"%".$val."%"):'';
            $this->assign('val',$val);
            A('Public')-> page('message', $where, 10, 'message_board','');
    }

    public function del_message(){
        if(I('id')){
            M('message')->where(array('id'=>I('id')))->delete()?$this->success('删除成功'):$this->error('删除失败');
        }else{
            $this->error('无此ID，操作失败');
        }
    }
}